home *** CD-ROM | disk | FTP | other *** search
Wrap
VERSION 2.00 Begin Form frmini BackColor = &H00C0C0C0& BorderStyle = 1 'Fixed Single Caption = ".INI File Read/Write" ClientHeight = 2100 ClientLeft = 3240 ClientTop = 3165 ClientWidth = 4215 ControlBox = 0 'False Height = 2790 Icon = INI.FRX:0000 Left = 3180 LinkTopic = "Form1" MaxButton = 0 'False MinButton = 0 'False ScaleHeight = 2100 ScaleWidth = 4215 Top = 2535 Width = 4335 Begin CommandButton cmdExplain Caption = "?" FontBold = -1 'True FontItalic = 0 'False FontName = "MS Sans Serif" FontSize = 13.5 FontStrikethru = 0 'False FontUnderline = 0 'False Height = 420 Left = 3195 TabIndex = 5 Top = 1575 Width = 780 End Begin Timer Timer1 Interval = 1000 Left = 3735 Top = 1575 End Begin CommandButton cmdQuit Caption = "&Quit" Height = 420 Left = 2385 TabIndex = 2 Top = 1575 Width = 780 End Begin Shape Shape1 Height = 510 Left = 225 Top = 1530 Width = 2085 End Begin Label Label3 Alignment = 2 'Center BackStyle = 0 'Transparent Caption = "Drag the window somewhere, quit, then run the demo again to see the changes take effect. (Note the new coordinates after the drag)" ForeColor = &H00000000& Height = 600 Left = 90 TabIndex = 4 Top = 855 Width = 4110 End Begin Label Label5 Alignment = 2 'Center BackStyle = 0 'Transparent Caption = "This window position is set according to values in the ini.ini file which are read on loading and, depending on the state of the Options menu, (which is an .ini item also), written on quitting." ForeColor = &H00000000& Height = 825 Left = 90 TabIndex = 3 Top = 45 Width = 4110 End Begin Label Label2 BackStyle = 0 'Transparent Caption = "Window Left:" ForeColor = &H000000FF& Height = 195 Left = 270 TabIndex = 1 Top = 1800 Width = 1905 End Begin Label Label1 BackStyle = 0 'Transparent Caption = "Window Top:" ForeColor = &H000000FF& Height = 195 Left = 270 TabIndex = 0 Top = 1575 Width = 1905 End Begin Menu mnuOptions Caption = "&Options" Begin Menu mnuOptionsSettings Caption = "&Save Settings on Exit" End End Begin Menu mnuExplain Caption = "&Explain" End 'Indents, tabulation & spacing in events is purely for readability Option Explicit 'This is optional but can help eliminated runtime variable bugs Dim lpAppName$, lpKeyName$ 'Dimension variables used. (Must be done if 'Option Explicit' is used) Dim lpDefault$, lpReturnString$ Dim lpString$, lpFileName$, bufflen%, nSize% Dim Truncated$, toppos$, leftpos$, WriteString% Dim c, lpClassName$, IsRunning 'See function FindWindow% below 'Declare the API Private read and write functions Declare Function GetPrivateProfileString% Lib "Kernel" (ByVal lpAppName$, ByVal lpKeyName$, ByVal lpDefault$, ByVal lpReturnString$, ByVal nSize%, ByVal lpFileName$) Declare Function WritePrivateProfileString% Lib "Kernel" (ByVal lpAppName$, ByVal lpKeyName$, ByVal lpString$, ByVal lpFileName$) 'The following function can tell you if an application 'is already running. In this instance it will look for Windows Notepad 'See the mnuExplain_Click event Declare Function FindWindow% Lib "user" (ByVal lpClassName As Any, ByVal lpCaption As Any) Sub cmdExplain_Click () mnuExplain_Click End Sub Sub cmdQuit_Click () If mnuOptionsSettings.Checked = True Then 'If 'Save Settings' ticked then toppos$ = Str$(frmini.Top) 'Convert property values to strings. leftpos$ = Str$(frmini.Left) '(Unfortunately this adds a leading space) lpAppName$ = "windowpos" 'Re-assign ini.ini AppName lpKeyName$ = "Top" 'Assign ini.ini key name lpString$ = Right$(toppos$, Len(toppos$) - 1) 'and strip leftmost space from parameter string 'Write new value to ini.ini WriteString% = WritePrivateProfileString(lpAppName$, lpKeyName$, lpString$, lpFileName$) lpKeyName$ = "Left" 'Assign ini.ini key name lpString$ = Right$(leftpos$, Len(leftpos$) - 1) 'and strip leftmost space from parameter string 'Write new value to ini.ini WriteString% = WritePrivateProfileString(lpAppName$, lpKeyName$, lpString$, lpFileName$) lpAppName$ = "options" 'Identify next ini.ini section lpKeyName$ = "Checked" 'Assign ini.ini key name lpString$ = "0" 'Save Settings is 'ticked' 'Write new value to ini.ini WriteString% = WritePrivateProfileString(lpAppName$, lpKeyName$, lpString$, lpFileName$) Else 'Else Save Settings is not 'ticked' lpAppName$ = "options" 'Identify ini.ini section lpKeyName$ = "Checked" 'Assign ini.ini key name lpString$ = "1" 'Save Settings is not 'ticked' 'Write new value to ini.ini WriteString% = WritePrivateProfileString(lpAppName$, lpKeyName$, lpString$, lpFileName$) End If End End Sub Sub Form_Load () 'For demo purposes the code is quite concise and no code optimisation has been attempted lpFileName$ = App.Path & "\ini.ini" 'Identify location and filename of ini.ini lpAppName$ = "windowpos" 'Identify section in ini.ini (e.g. heading enclosed in [] ) lpDefault$ = "0" 'Set a default for the value on the right of = if it is not found lpReturnString$ = Space$(10) 'Give the returned string a arbitrary length value nSize% = Len(lpReturnString$) 'Set returned string buffer to this length lpKeyName$ = "Top" 'Define key name on left of = 'Call the read function placing the returned string length into bufflen% bufflen% = GetPrivateProfileString(lpAppName$, lpKeyName$, lpDefault$, lpReturnString$, nSize%, lpFileName$) Truncated$ = Left$(lpReturnString$, bufflen%) 'Strip unwanted space from right of returned string together with null character Label1.Caption = "Window Top: " & Truncated$ 'Show Top value in Label1.Caption (For demo purposes only) frmini.Top = Val(Truncated$) 'Set frmini.Top property lpKeyName$ = "Left" 'Redefine key name value on left of = 'Call the read function placing the returned string length into bufflen% bufflen% = GetPrivateProfileString(lpAppName$, lpKeyName$, lpDefault$, lpReturnString$, nSize%, lpFileName$) Truncated$ = Left$(lpReturnString$, bufflen%) 'Strip unwanted spaces from right of returned string together with null character Label2.Caption = "Window Left: " & Truncated$ 'Show Left value in Label2.Caption (For demo purposes only) frmini.Left = Val(Truncated$) 'Set frmini.Left property lpAppName$ = "options" 'Identify section in ini.ini lpKeyName$ = "Checked" 'Redefine key name value on left of = 'lpDefault$ = "0" 'This doesn't change 'Call the read function placing the returned string length into bufflen% bufflen% = GetPrivateProfileString(lpAppName$, lpKeyName$, lpDefault$, lpReturnString$, nSize%, lpFileName$) Truncated$ = Left$(lpReturnString$, bufflen%) 'Strip unwanted spaces from right of returned string together with null character If Truncated$ = "0" Then 'Toggle 'checked' status of menu item mnuOptionsSettings.Checked = True Else mnuOptionsSettings.Checked = False End If End Sub Sub mnuExplain_Click () 'Check for instance of Windows Notepad lpClassName$ = "Notepad" 'Identify Application to be checked for instance of IsRunning = FindWindow(lpClassName$, 0&) 'Call API function If IsRunning <> 0 Then 'Application is already running MsgBox "Notepad is already running !", 64, ".INI File Read/Write" 'Tell user Exit Sub 'and exit Else 'Else run it and load the ini.txt file c = Shell("Notepad.exe " & App.Path & "\ini.txt", 3) '3 = Window maximised SendKeys "%+(EW)", True 'Set WordWrap to on (Command word "True" 'ensures that keys sent are processed fully before End If 'control is returned to this sub procedure 'False = control returns immediately keys are sent) End Sub Sub mnuOptionsSettings_Click () 'Is 'Save Settings On Exit' ticked ? If mnuOptionsSettings.Checked = True Then 'Toggle checked status mnuOptionsSettings.Checked = False Else mnuOptionsSettings.Checked = True End If End Sub Sub Timer1_Timer () 'The Timer control is included only for the purposes of the demo 'Ensure correct values are displayed when window has been dragged Label1.Caption = "Window Top = " & frmini.Top Label2.Caption = "Window Left = " & frmini.Left 'Repeats every second (i.e Timer1.Interval = 1000) End Sub